package modes

import (
	"fmt"
	"time"
)

/*班主任信息*/
type TeacInfo struct {
	Id       int64  `json:"teac_id" xorm:"id"`             //班主任信息id
	UId      int64  `json:"teac_uid" xorm:"uid"`           //班主任id
	Phone    string `json:"teac_phone" xorm:"phone"`       //手机号
	QQ       int64  `json:"teac_qq" xorm:"qq"`             //qq号
	WX       string `json:"teac_wx" xorm:"wx"`             //微信
	UName    string `json:"teac_u_name" xorm:"u_name"`     //老师名称
	PName    string `json:"teac_p_name" xorm:"p_name"`     //项目名称
	Note     string `json:"teac_note" xorm:"note"`         //寄语
	Portrait string `json:"teac_portrait" xorm:"portrait"` //头像
	At       int64  `json:"teac_at" xorm:"at"`             //创建时间
}

func (this *TeacInfo) TableName() string {
	return "teacinfo"
}
func (this *TeacInfo) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *TeacInfo) Get() (bool, error) {
	return Db(0).Get(this)
}
func (this *TeacInfo) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *TeacInfo) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *TeacInfo) Count(where string) (int64, error) {
	var val Collen
	return Db(0).Where(where).Count(&val)
}
func (this *TeacInfo) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *TeacInfo) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *TeacInfo) where(fage, count, page int, where, field string) ([]TeacInfo, error) {
	list := make([]TeacInfo, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
func (this *TeacInfo) Lists(uid int64) ([]TeacInfo, error) {
	list := make([]TeacInfo, 0)
	var err error
	err = Db(0).Table("teacinfo").
		Where("uid = ?", uid).
		Find(&list)
	return list, err
}
